package cn.gorit.admin.service;

import cn.gorit.common.enums.ResultEnums;
import cn.gorit.common.lang.Result;
import cn.gorit.entity.resource.Source;
import cn.gorit.mapper.resource.SourceMapper;
import cn.gorit.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version v1.0
 * @Classname ResourceService
 * @Description TODO
 * @Author CodingGorit
 * @Created on 2021/4/16 0:22
 * @IDLE IntelliJ IDEA
 **/
@Service
public class ResourceService {

    @Autowired
    private SourceMapper mapper;

    public Result<Page<Source>> getByPage(Page<Source> page) {
        List<Source> list = mapper.getByPage(page);
        page.setList(list);
        page.setTotalCount(getByPageCount(page));
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),page);
    }

    public Integer getByPageCount(Page<Source> page) {
        return mapper.getByPageCount(page);
    }

    public Result<Source> getSourceById(Integer id) {
        Source source = mapper.getSourceDetailById(id);
        String BASE_PATH = "http://cloud-myblog.oss-cn-shanghai.aliyuncs.com/";
        source.setSourceUrl(BASE_PATH + source.getSourceUrl());
        return new Result<>(ResultEnums.QUERY_SUCCESS.getCode(),ResultEnums.QUERY_SUCCESS.getMsg(),source);
    }
}
